package foundation.graph;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class PrimAlgorithm {

    public static void main(String[] args) {
        List<List<int[]>> graph = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i <= n; i++) {
            graph.add(new ArrayList<>());
        }
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            int w = sc.nextInt();
            graph.get(u).add(new int[]{v, w});
            graph.get(v).add(new int[]{u, w});
        }

        PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
        for (int[] edge : graph.get(1)) {
            heap.add(edge);
        }
        boolean[] set = new boolean[n + 1];
        int nodeCnt = 1;
        int ans = 0;
        set[1] = true;
        while (!heap.isEmpty()) {
            int[] edge = heap.poll();
            int next = edge[0];
            int cost = edge[1];
            if (!set[next]) {
                nodeCnt++;
                set[next] = true;
                ans += cost;
                for (int[] e : graph.get(next)) {
                    heap.add(e);
                }
            }
        }
        if (nodeCnt == n) {
            System.out.println(ans);
        } else {
            System.out.println("orz");
        }
    }




}
